import Vue from 'vue'
import VueRouter from 'vue-router'
import Homepage from "@/views/reception/homepage/index.vue";
import AboutUs from "@/views/reception/aboutUs/index.vue";
import ContactUs from "@/views/reception/contactUs/index.vue";
import Feedback from "@/views/reception/feedback/index.vue";
import Community from "@/views/reception/community/index.vue";
import UserBackground from "@/views/reception/userBackground/index.vue";
import Volunteer from "@/views/reception/userBackground/volunteer/index.vue";
import FeedbackManage from "@/views/reception/userBackground/feedbackManage/index.vue";
import BiddingManage from "@/views/reception/userBackground/biddingManage/index.vue";

import ActivityManage from "@/views/reception/userBackground/activityManage/index.vue";
import Applied from "@/views/reception/userBackground/vehicleManage/applied/index.vue";
import ApplyNow from "@/views/reception/userBackground/vehicleManage/applyNow/index.vue";
import VehicleDetailUser from "@/views/reception/userBackground/vehicleManage/vehicleDetailUser/index.vue";
import AdminBackground from "@/views/adminBackground/index.vue";
import UserLayout from "@/layout/userLayout/index.vue";
import AdminLayout from "@/layout/adminLayout/index.vue";
import AdminManage from "@/views/adminBackground/peopleManage/adminManage/index.vue";
import UserManage from "@/views/adminBackground/peopleManage/userManage/index.vue";
import ReleaseNotice from "@/views/adminBackground/noticeManage/releaseNotice/index.vue";
import NoticeReleased from "@/views/adminBackground/noticeManage/noticeReleased/index.vue";
import ReleaseActivity from "@/views/adminBackground/activityAdminManage/releaseActivity/index.vue";
import ActivityReleased from "@/views/adminBackground/activityAdminManage/activityReleased/index.vue";

import PendingVehicle from "@/views/adminBackground/vehicleManage/pendingVehicle/index.vue";
import ProcessedVehicle from "@/views/adminBackground/vehicleManage/processedVehicle/index.vue";
import PendingFeedback from "@/views/adminBackground/feedbackManage/pendingFeedback/index.vue";
import ProcessedFeedback from "@/views/adminBackground/feedbackManage/processedFeedback/index.vue";
import AddAdmin from "@/views/adminBackground/peopleManage/addAdmin/index.vue";
import NoticeDetail from "@/views/adminBackground/noticeManage/noticeDetail/index.vue";
import ActivityDetail from "@/views/adminBackground/activityAdminManage/activityDetail/index.vue";
import BiddingDetail from "@/views/adminBackground/biddingAdminManage/biddingDetail/index.vue";
import TenderBidding from "@/views/adminBackground/biddingAdminManage/tenderBidding/index.vue";
import TenderReleased from "@/views/adminBackground/biddingAdminManage/tenderReleased/index.vue";
import TenderDetail from "@/views/adminBackground/biddingAdminManage/tenderDetail/index.vue";
import VehicleAudit from "@/views/adminBackground/vehicleManage/vehicleAudit/index.vue";
import VehicleDetail from "@/views/adminBackground/vehicleManage/vehicleDetail/index.vue";
import FeedBackDetail from "@/views/adminBackground/feedbackManage/feedBackDetail/index.vue";
import FeedReplyDetail from "@/views/adminBackground/feedbackManage/feedReplyDetail/index.vue";
import axios from "axios";
import UserFeedReplyDetail from "@/views/reception/userBackground/userFeedReplyDetail/index.vue";
import UserActivityDetail from "@/views/reception/userBackground/userActivityDetail/index.vue";
import MoreInformation from "@/views/reception/moreInformation/index.vue";
import ActivityParticulars from "@/views/reception/particulars/activityParticulars/index.vue";
import BinddingParticulars from "@/views/reception/particulars/binddingParticulars/index.vue";
import NoticeParticulars from "@/views/reception/particulars/noticeParticulars/index.vue";
import VolunteerParticulars from "@/views/reception/particulars/volunteerParticulars/index.vue";
import AddBidding from "@/views/reception/particulars/binddingParticulars/addBidding/index.vue";
import {addRegistration} from "@/api/allApi";
import AddRegistration from "@/views/reception/particulars/activityParticulars/addRegistration/index.vue";
import UserInfo from "@/views/reception/userBackground/userInfo/index.vue";
import MyImage from "@/views/adminBackground/MyImage.vue";
import ApplyDetail from "@/views/adminBackground/activityAdminManage/applyDetail/index.vue";


Vue.use(VueRouter)
// 解决ElementUI导航栏中的vue-router在3.0版本以上重复点菜单报错问题
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
    return originalPush.call(this, location).catch(err => err)
}


const routes = [
    {
        path:'/',
        component:UserLayout,
        redirect:'/userLayout',
        meta:{isAuth:false,title:'佛山市小布村'},
    },
    {
        path:'/userLayout',
        name:'userLayout',
        component:UserLayout,
        redirect:'/userLayout/homepage',
        meta:{isAuth:false},
        children: [
            {
                path: 'homepage',
                name: 'homepage',
                component: Homepage,
                meta:{isAuth:false,keepAlive: true,title:'佛山市小布村'}
            },
            {
                path: 'aboutUs',
                name: 'aboutUs',
                component: AboutUs,
                meta:{isAuth:false,title:'佛山市小布村'}
            },
            {
                path: 'contactUs',
                name: 'contactUs',
                component: ContactUs,
                meta:{isAuth:false,keepAlive: true,title:'佛山市小布村'}
            },
            {
                path: 'feedback',
                name: 'feedback',
                component: Feedback,
                meta:{isAuth:false,title:'佛山市小布村'}
            },
            {
                path: 'community',
                name: 'community',
                component: Community,
                meta:{isAuth:false,title:'佛山市小布村'}
            },
            {
                path: 'moreInformation',
                name: 'moreInformation',
                component: MoreInformation,
                meta:{isAuth:false,keepAlive: true,title:'佛山市小布村'}
            },
            {
                path: 'activityParticulars/:activityId',
                name: 'activityParticulars',
                component: ActivityParticulars,
                meta:{isAuth:false,title:'佛山市小布村'}
            },
            {
                path: 'binddingParticulars/:tenderId',
                name: 'binddingParticulars',
                component: BinddingParticulars,
                meta:{isAuth:false,title:'佛山市小布村'}
            },
            {
                path: 'addBidding/:tenderId',
                name: 'addBidding',
                component: AddBidding,
                meta:{isAuth:false,title:'佛山市小布村'}
            },
            {
                path: 'addRegistration/:activityId/:activityId',
                name: 'addRegistration',
                component: AddRegistration,
                meta:{isAuth:false,title:'佛山市小布村'}
            },
            {
                path: 'noticeParticulars/:noticeId',
                name: 'noticeParticulars',
                component: NoticeParticulars,
                meta:{isAuth:false,title:'佛山市小布村'}
            },
            {
                path: 'volunteerParticulars/:activityId',
                name: 'volunteerParticulars',
                component: VolunteerParticulars,
                meta:{isAuth:false,title:'佛山市小布村'}
            },


            {
                path: 'userBackground',
                name: 'userBackground',
                component: UserBackground,
                redirect:'/userLayout/userBackground/applyNow',
                meta:{isAuth:true,title:'佛山市小布村'},
                children:[
                    {
                        path: 'applyNow',
                        name: 'applyNow',
                        component: ApplyNow,
                        meta:{isAuth:true,title:'佛山市小布村'},
                    },
                    {
                        path: 'applied',
                        name: 'applied',
                        component: Applied,
                        meta:{isAuth:true,title:'佛山市小布村'},
                    },
                    {
                        path: 'userInfo',
                        name: 'userInfo',
                        component: UserInfo,
                        meta:{isAuth:true,title:'佛山市小布村'},
                    },
                    {
                        path: 'vehicleDetailUser/:veApplyId',
                        name: 'VehicleDetailUser',
                        component: VehicleDetailUser,
                        meta:{isAuth:true,title:'佛山市小布村'},
                    },
                    {
                        path: 'biddingManage',
                        name: 'biddingManage',
                        component: BiddingManage,
                        meta:{isAuth:true,title:'佛山市小布村'},
                    },
                    {
                        path: 'activityManage',
                        name: 'activityManage',
                        component: ActivityManage,
                        meta:{isAuth:true,title:'佛山市小布村'},
                    },
                    {
                        path: 'volunteer',
                        name: 'volunteer',
                        component: Volunteer,
                        meta:{isAuth:true,title:'佛山市小布村'},
                    },
                    {
                        path: 'feedbackManage',
                        name: 'feedbackManage',
                        component: FeedbackManage,
                        meta:{isAuth:true,title:'佛山市小布村'},
                    },
                    {
                        path: 'userFeedReplyDetail/:replyId',
                        name: 'userFeedReplyDetail',
                        component: UserFeedReplyDetail,
                        meta:{isAuth:true,title:'佛山市小布村'},
                    },
                    {
                        path: 'userActivityDetail/:registrationId',
                        name: 'userActivityDetail',
                        component: UserActivityDetail,
                        meta:{isAuth:true,title:'佛山市小布村'},
                    }
                ]
            },
        ]
    },

    {
        path: '/adminLayout',
        name: 'adminLayout',
        component: AdminLayout,
        redirect:'/adminLayout/adminBackground/image',
        meta:{isAuth:true,title:'佛山市小布村管理平台'},
        children: [
            {
                path: 'adminBackground',
                name: 'adminBackground',
                component: AdminBackground,
                redirect:'/adminLayout/adminBackground/image',
                meta:{isAuth:true,title:'佛山市小布村管理平台'},
                children:[
                    {
                        path: 'adminManage',
                        name: 'adminManage',
                        component: AdminManage,
                        meta:{isAuth:true,title:'佛山市小布村管理平台'},
                    },
                    {
                        path: 'userManage',
                        name: 'userManage',
                        component: UserManage,
                        meta:{isAuth:true,title:'佛山市小布村管理平台'},
                    },
                    {
                        path: 'releaseNotice',
                        name: 'releaseNotice',
                        component: ReleaseNotice,
                        meta:{isAuth:true,title:'佛山市小布村管理平台'},
                    },
                    {
                        path: 'noticeReleased',
                        name: 'noticeReleased',
                        component: NoticeReleased,
                        meta:{isAuth:true,title:'佛山市小布村管理平台'},
                    },
                    {
                        path: 'noticeDetail/:noticeId',
                        name: 'noticeDetail',
                        component: NoticeDetail,
                        meta:{isAuth:true,title:'佛山市小布村管理平台'},
                    },
                    {
                        path: 'releaseActivity',
                        name: 'releaseActivity',
                        component: ReleaseActivity,
                        meta:{isAuth:true,title:'佛山市小布村管理平台'},
                    },
                    {
                        path: 'activityReleased',
                        name: 'activityReleased',
                        component: ActivityReleased,
                        meta:{isAuth:true,title:'佛山市小布村管理平台'},
                    },
                    {
                        path: 'activityDetail/:activityId',
                        name: 'activityDetail',
                        component: ActivityDetail,
                        meta:{isAuth:true,title:'佛山市小布村管理平台'},
                    },
                    {
                        path: 'applyDetail/:activityId',
                        name: 'applyDetail',
                        component: ApplyDetail,
                        meta:{isAuth:true,title:'佛山市小布村管理平台'},
                    },
                    {
                        path: 'tenderBidding',
                        name: 'tenderBidding',
                        component: TenderBidding,
                        meta:{isAuth:true,title:'佛山市小布村管理平台'},
                    },
                    {
                        path: 'tenderReleased',
                        name: 'tenderReleased',
                        component: TenderReleased,
                        meta:{isAuth:true,title:'佛山市小布村管理平台'},
                    },
                    {
                        path: 'biddingDetail/:tenderId',
                        name: 'biddingDetail',
                        component: BiddingDetail,
                        meta:{isAuth:true,title:'佛山市小布村管理平台'},
                    },
                    {
                        path: 'tenderDetail/:tenderId',
                        name: 'tenderDetail',
                        component: TenderDetail,
                        meta:{isAuth:true,title:'佛山市小布村管理平台'},
                    },
                    {
                        path: 'pendingVehicle',
                        name: 'pendingVehicle',
                        component: PendingVehicle,
                        meta:{isAuth:true,title:'佛山市小布村管理平台'},
                    },
                    {
                        path: 'processedVehicle',
                        name: 'processedVehicle',
                        component: ProcessedVehicle,
                        meta:{isAuth:true,title:'佛山市小布村管理平台'},
                    },
                    {
                        path: 'vehicleAudit/:veApplyId',
                        name: 'vehicleAudit',
                        component: VehicleAudit,
                        meta:{isAuth:true,title:'佛山市小布村管理平台'},
                    },
                    {
                        path: 'vehicleDetailUser/:veApproId',
                        name: 'VehicleDetailUser',
                        component: VehicleDetail,
                        meta:{isAuth:true,title:'佛山市小布村管理平台'},
                    },
                    {
                        path: 'pendingFeedback',
                        name: 'pendingFeedback',
                        component: PendingFeedback,
                        meta:{isAuth:true,title:'佛山市小布村管理平台'},
                    },
                    {
                        path: 'processedFeedback',
                        name: 'processedFeedback',
                        component: ProcessedFeedback,
                        meta:{isAuth:true,title:'佛山市小布村管理平台'},
                    },
                    {
                        path: 'feedBackDetail/:feedId',
                        name: 'feedBackDetail',
                        component: FeedBackDetail,
                        meta:{isAuth:true,title:'佛山市小布村管理平台'},
                    },
                    {
                        path: 'feedReplyDetail/:replyId',
                        name: 'feedReplyDetail',
                        component: FeedReplyDetail,
                        meta:{isAuth:true,title:'佛山市小布村管理平台'},
                    },
                    {
                        path: 'addAdmin',
                        name: 'addAdmin',
                        component: AddAdmin,
                        meta:{isAuth:true,title:'佛山市小布村管理平台'},
                    },
                    {
                        path: 'image',
                        name: 'image',
                        component: MyImage,
                        meta:{isAuth:true,title:'佛山市小布村管理平台'},
                    }
                ]
            }
        ]
    },

]

const router = new VueRouter({
    mode: 'history',
    base: process.env.BASE_URL,
    routes
})

router.beforeEach((to, from, next) => {
    if (!to.meta.isAuth) { //判断是否需要鉴权
        // window.localStorage.removeItem('access-admin')
        next()
    } else {
        let admin = JSON.parse(window.localStorage.getItem('access-admin'))
        let user = JSON.parse(window.localStorage.getItem('access-user'))
        if (!admin && !user) {
            next({path: '/'})
        } else {
            if(admin != null){
                //校验token合法性
                axios({
                    url:'http://localhost:9090/checkToken',
                    method:'post',
                    headers:{
                        token:admin.token
                    }
                }).then((response) => {
                    // console.log(response.data)
                    if(!response.data){
                        // console.log('校验失败')
                        localStorage.removeItem('access-admin');
                        next({name: 'userLayout'})
                    }
                })
                next()
            }else if (user != null){
                //校验token合法性
                axios({
                    url:'http://localhost:9090/checkToken',
                    method:'post',
                    headers:{
                        token:user.token
                    }
                }).then((response) => {
                    if(!response.data){
                        console.log('校验失败')
                        localStorage.removeItem('access-user')
                        next({name: 'userLayout'})
                    }
                })
                next()
            }
        }
    }
})






export default router
